package cn.iocoder.yudao.module.erp.controller.admin.business.order.orderdelivery;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.erp.controller.admin.business.order.orderdelivery.vo.OrderDeliveryPageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.business.order.orderdelivery.vo.OrderDeliveryRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.business.order.orderdelivery.vo.OrderDeliverySaveReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.business.order.orderdelivery.OrderDeliveryDO;
import cn.iocoder.yudao.module.erp.service.business.order.orderdelivery.OrderDeliveryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 交货方式")
@RestController
@RequestMapping("/erp/order-delivery")
@Validated
public class OrderDeliveryController {

    @Resource
    private OrderDeliveryService orderDeliveryService;

    @PostMapping("/create")
    @Operation(summary = "创建交货方式")
    @PreAuthorize("@ss.hasPermission('erp:order-delivery:create')")
    public CommonResult<Long> createOrderDelivery(@Valid @RequestBody OrderDeliverySaveReqVO createReqVO) {
        return success(orderDeliveryService.createOrderDelivery(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新交货方式")
    @PreAuthorize("@ss.hasPermission('erp:order-delivery:update')")
    public CommonResult<Boolean> updateOrderDelivery(@Valid @RequestBody OrderDeliverySaveReqVO updateReqVO) {
        orderDeliveryService.updateOrderDelivery(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除交货方式")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:order-delivery:delete')")
    public CommonResult<Boolean> deleteOrderDelivery(@RequestParam("id") Long id) {
        orderDeliveryService.deleteOrderDelivery(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得交货方式")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:order-delivery:query')")
    public CommonResult<OrderDeliveryRespVO> getOrderDelivery(@RequestParam("id") Long id) {
        OrderDeliveryDO orderDelivery = orderDeliveryService.getOrderDelivery(id);
        return success(BeanUtils.toBean(orderDelivery, OrderDeliveryRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得交货方式分页")
    @PreAuthorize("@ss.hasPermission('erp:order-delivery:query')")
    public CommonResult<PageResult<OrderDeliveryRespVO>> getOrderDeliveryPage(@Valid OrderDeliveryPageReqVO pageReqVO) {
        PageResult<OrderDeliveryDO> pageResult = orderDeliveryService.getOrderDeliveryPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, OrderDeliveryRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出交货方式 Excel")
    @PreAuthorize("@ss.hasPermission('erp:order-delivery:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportOrderDeliveryExcel(@Valid OrderDeliveryPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<OrderDeliveryDO> list = orderDeliveryService.getOrderDeliveryPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "交货方式.xls", "数据", OrderDeliveryRespVO.class,
                        BeanUtils.toBean(list, OrderDeliveryRespVO.class));
    }

}